package com.rocksea.app.fragment;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.rocksea.app.Account;
import com.rocksea.app.MyApplication;
import com.rocksea.app.R;
import com.rocksea.app.entry.CheckVersion;
import com.rocksea.app.event.UpDateVersionEvent;
import com.rocksea.app.http.HttpUtil;
import com.rocksea.app.http.UpDateTask;
import com.rocksea.app.service.DownloadService;
import com.rocksea.app.utils.PerUtils;
import com.rocksea.app.utils.UiUtils;
import com.rocksea.app.activity.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 吴志华 on 2017/7/20.
 * 用户中心
 */

public class UserFragment extends Fragment implements Switch.OnCheckedChangeListener,UpDateTask.CheckVersionCallBack{

    @InjectView(R.id.User_Name)
    TextView userName;
    private ProgressDialog dialog;

    @OnClick(R.id.Machine)
    public void machine(View v)
    {
        mAutoUpLoadSwitch.setChecked(!mAutoUpLoadSwitch.isChecked());
    }

    @InjectView(R.id.Auto_UpLoad)
    Switch mAutoUpLoadSwitch;

    @OnClick(R.id.APP)
    public void app(View v)
    {
        UiUtils.showToast("武汉岩海工程技术有限公司");
    }
    @InjectView(R.id.Image_AutoLoad)
    Switch mImageAutoLoadSwitch;
    @InjectView(R.id.Version_Name)
    TextView mVersionName;

    @OnClick(R.id.Image_UpLoad)
    public void imageUpLoad(View view)
    {
        mImageAutoLoadSwitch.setChecked(!mImageAutoLoadSwitch.isChecked());
    }


    @OnClick(R.id.Version)
    public void version(View v)
    {
        checkLastVersion();
    }
    @OnClick(R.id.Logout)
    public void logout(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("是否确定注销当前账户?");
        builder.setTitle("提示");
        builder.setPositiveButton("注销", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                UiUtils.JumpActivity(getContext(),LoginActivity.class);
                getActivity().finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_user,container,false);
        ButterKnife.inject(this,mRootView);

        String version = UiUtils.getVersion();
        mVersionName.setText(String.format("版本号(V%s_%s)",version==null?"UNKOWN":version,MyApplication.UPDATE_TIME));
        if (Account.getAccount()!=null)
            userName.setText(Account.getAccount());
        mImageAutoLoadSwitch.setChecked(HttpUtil.isImageAutoUpload());
        mAutoUpLoadSwitch.setChecked(HttpUtil.isFileAutoUpload());
        mAutoUpLoadSwitch.setOnCheckedChangeListener(this);
        mImageAutoLoadSwitch.setOnCheckedChangeListener(imageSwitch);
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
    }

    public static UserFragment newInstance()
    {
        return new UserFragment();
    }

    private void checkLastVersion()
    {
        dialog = ProgressDialog.show(getContext(),"","正在检查更新...",true);
        dialog.show();
        UpDateTask.checkUpDate(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        PerUtils.setBoolean(Account.AUTO_UPLOAD,isChecked);
    }

    private Switch.OnCheckedChangeListener imageSwitch = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            PerUtils.setBoolean(Account.IMAGE_AUTO_UPLOAD,isChecked);
        }
    };

    @Override
    public void onCheckVersionCallBack(final CheckVersion.DataBean.ListBean checkInfo)
    {
        if (dialog!=null)
            dialog.dismiss();
        String versionName = UiUtils.getVersion();
        if (versionName == null || checkInfo==null || versionName.equals(checkInfo.getVersionName()))
        {
            UiUtils.showToast("已是最新版本");
            String version = UiUtils.getVersion();
            mVersionName.setText(String.format("版本号(V%s_%s)",version==null?"UNKOWN":version,MyApplication.UPDATE_TIME));
            return;
        }
        mVersionName.setTextColor(ContextCompat.getColor(getContext(),android.R.color.holo_red_dark));
        mVersionName.setText(String.format("发现新版本(%s)",checkInfo.getVersionCode()));
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("发现新版本");
        builder.setMessage(checkInfo.getDescription());
        builder.setCancelable(false);
        builder.setPositiveButton("马上更新", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                Intent intent = new Intent(getContext(),DownloadService.class);
                intent.setData(Uri.parse(checkInfo.getAppUrl()));
                getContext().startService(intent);
            }
        });
        builder.setNegativeButton("忽略", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onUpdateVersion(UpDateVersionEvent upDateVersionEvent)
    {
        mVersionName.setText("发现新版本");
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
